﻿<div id="gridOrgs"></div>
<div id="dlgAddOrg" style="display:none;padding-left:10px;">
    <form id="formAddOrg"></form>
</div>
<div id="dlgUpdateOrg" style="display:none;padding-left:10px;">
    <form id="formUpdateOrg"></form>
</div>

<script type="text/javascript">
    var gridOrgs = null;
    var dlgAddOrg = null;
    var dlgUpdateOrg = null;
    var formAddOrg = null;
    var formUpdateOrg = null;

    function addOrg() {
        $.post('/API/Org/Create', formAddOrg.getData(), function (data) {
            if (data.code == 0) {
                $.ligerDialog.success('添加成功', '提示');
                gridOrgs.reload();
                dlgAddOrg.hide();
            } else {
                $.ligerDialog.error(data.msg, '错误');
            }
        }, 'json');
    }

    function updateOrg() {
        $.post('/API/Org/Update', formUpdateOrg.getData(), function (data) {
            if (data.code == 0) {
                $.ligerDialog.success('修改成功', '提示');
                gridOrgs.reload();
                dlgUpdateOrg.hide();
            } else {
                $.ligerDialog.error(data.msg, '错误');
            }
        }, 'json');
    }

    function deleteOrg(row) {
        $.post('/API/Org/Delete', row,
            function (data) {
                if (data.code == 0) {
                    $.ligerDialog.success('删除成功', '提示');
                    gridOrgs.reload();
                } else {
                    $.ligerDialog.error(data.msg, '错误');
                }
            }, 'json');
    }

    function showDlgAddOrg() {
        if (dlgAddOrg) {
            dlgAddOrg.show();
        }
        else {
            dlgAddOrg = $.ligerDialog.open({
                target: $('#dlgAddOrg'),
                width: 320,
                height: 200,
                title: '添加新部门',
                isResize: true,
                modal: true,
                buttons: [
                    {
                        text: '确定', onclick: function () {
                            if (formAddOrg.valid()) {
                                addOrg();
                            }
                            else {
                                formAddOrg.showInvalid();
                            }
                        }
                    },
                    {
                        text: '取消', onclick: function () {
                            dlgAddOrg.hide();
                        }
                    }
                ]
            });
        }

        //liger.get('cboParentOrgId').set({ Rows: gridOrgs.getData() });
        liger.get('cboParentOrgId').reload();
    }

    function showDlgUpdateOrg(row) {
        if (dlgUpdateOrg) {
            dlgUpdateOrg.show();
        }
        else {
            dlgUpdateOrg = $.ligerDialog.open({
                target: $('#dlgUpdateOrg'),
                width: 320,
                height: 200,
                title: '修改部门',
                isResize: true,
                modal: true,
                buttons: [
                    {
                        text: '确定', onclick: function () {
                            if (formUpdateOrg.valid()) {
                                updateOrg();
                            }
                            else {
                                formUpdateOrg.showInvalid();
                            }
                        }
                    },
                    {
                        text: '取消', onclick: function () {
                            dlgUpdateOrg.hide();
                        }
                    }
                ]
            });
        }

        formUpdateOrg.setData(row);
    }

    function itemclick(item) {
        switch (item.method) {
            case 'add':
                showDlgAddOrg();
                break;

            case 'update':
                DoAction(gridOrgs, function (row) {
                    showDlgUpdateOrg(row);
                }, '请选择需要修改的部门');
                break;

            case 'delete':
                DoActionConfirm(gridOrgs, deleteOrg, '请选择需要修删除的部门', '是否要删除当前选择的部门？');
                break;
        }
    }

    var toolbarOrgs = {
        items: [
            { method: 'add', text: '增加', click: itemclick, icon: 'add' },
            { line: true },
            { method: 'update', text: '修改', click: itemclick, icon: 'modify' },
            { line: true },
            { method: 'delete', text: '删除', click: itemclick, icon: 'delete' }
        ]
    };

    var columnOrgs = [
        { display: '部门名', name: 'OrgName', align: 'left', width: 200, minwidth: 120 }
    ];

    function initForm() {
        formAddOrg = $('#formAddOrg').ligerForm({
            inputWidth: 170, labelWidth: 90, space: 40,
            fields: [
                { display: '部门名称', name: 'OrgName', newline: true, type: 'text', validate: { required: true } },
                {
                    display: "上级部门", name: "ParentOrgId", comboboxName: 'cboParentOrgId',
                    newline: true, type: "combobox", editor: {
                        selectBoxWidth: 600,
                        selectBoxHeight: 300,
                        textField: 'OrgName',
                        valueField: 'Id',
                        grid: {
                            columns: columnOrgs,
                            rownumbers: true,
                            usePager: false,
                            tree: {
                                columnName: 'OrgName',
                                idField: 'Id',
                                parentIDField: 'ParentOrgId'
                            },
                            url: '/API/Org/Query'
                        }
                    }
                }
            ],
            validate: {}
        });

        formUpdateOrg = $('#formUpdateOrg').ligerForm({
            inputWidth: 170, labelWidth: 90, space: 40,
            fields: [
                { name: 'Id', type: 'hidden' },
                { display: '部门名称', name: 'OrgName', newline: true, type: 'text', validate: { required: true } }
            ],
            validate: {}
        });
    }

    $(document).ready(function () {
        $.metadata.setType("attr", "validate");
        initForm();
        gridOrgs = $('#gridOrgs').ligerGrid({
            title: '部门信息',
            columns: columnOrgs,
            toolbar: toolbarOrgs,
            rownumbers: true,
            usePager: false,
            resizable: false,
            frozen: false,
            tree: {
                columnName: 'OrgName',
                idField: 'Id',
                parentIDField: 'ParentOrgId'
            },
            url: '/API/Org/Query',
            method: 'post'
        });
    });
</script>